<?php
// DB Created by Rich L. a member in service in Northern Delaware.  Please feel free to share this code wherever it may be useful to alcholoics.  Should you have questons please contact me at sobernerd@gmail.com
session_start();

//Sedurity Check, is a valid user logged in? AND if not then display error message
if ($_SESSION['logged_in'] == 0) {
	require ('security_check.php');
	} 	
else 
	{
$returnTo = $_GET['return_to'];
if ($_GET['original_page']) {
	$original_page = '&original_page='.$_GET['original_page'];
}
 ?>
<script language="javascript" type="text/javascript">
	document.title = '<?php echo SITE_NAME ?> Add or Edit Group';
</script>
<?php
	// Get form information if editing
	$id = $_GET['id'];
	$return_id = $_GET['return_id'];
	$id_url_var = '&id='.$return_id;
	// set header assume new Group and change below if not
	$pageHeader = 'New Group';
	
	if ($id) {
		$pageHeader = 'Group Edit';
		$sql = "SELECT * FROM groups WHERE id='".$_GET['id']."'";
		$result = mysql_query($sql)
		or die(mysql_error());

		// Get each row of data on each iteration until
		// there is no more rows
		while ($row = mysql_fetch_assoc($result)) {
			$id = $row['id'];
			$intergroupid = $row['intergroupid'];
			$groupname = $row['groupname'];
			$intergroup_rep_id = $row['intergroup_rep_id'];
			$location_id = $row['location_id'];
			$ir_fname = $row['ir_fname'];
			$ir_lname = $row['ir_lname'];
			$ir_home_phone = $row['ir_home_phone'];
			$ir_cell_phone = $row['ir_cell_phone'];
			$ir_work_phone = $row['ir_work_phone'];
			$ir_email = $row['ir_email'];
			$lasteditor = $row['editor'];	
			$editdate = $row['editdate'];
		}
	}
?>

<form id="groupsform" action="processTables.php?table=groups&returnTo=<?php echo $returnTo.$id_url_var.$original_page?>" method="post" name="groupsform">
  <table class="form-table">
    <thead class="meetingHeader">
      <tr class="titleRow">
        <th colspan="3"><h3><?php echo $pageHeader ?></h3></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="labelRight"><label>Group Name:</label></td>
        <td colspan="2"><input type="text" name="groupname" value="<?php echo $groupname ?>" <?php if ($return_to=='intergroup_reps.php') {echo "readonly=\"readonly\"";}?>='<?php if ($return_to=='intergroup_reps.php') {echo "readonly=\"readonly\"";}?>' size="50" maxlength="75" /></td>
      </tr>
      <tr>
        <td class="labelRight"><label>Intergroup Rep</label></td>
        <td colspan="2"></td>
      </tr>
      <tr>
        <td class="labelRight"><label> First Name:</label></td>
        <td><input type="text" name="ir_fname" value="<?php echo $ir_fname ?>" size="15" maxlength="15" /></td>
      </tr>
      <tr>
        <td class="labelRight"><label> Last Name:</label></td>
        <td><input type="text" name="ir_lname" value="<?php echo $ir_lname ?>" size="15" maxlength="25" /></td>
      </tr>
      <tr>
        <td class="labelRight"><label> Home Phone:</label></td>
        <td><input type="text" name="ir_home_phone" value="<?php echo $ir_home_phone ?>" size="15" maxlength="15" /></td>
      </tr>
      <tr>
        <td class="labelRight"><label> Cell Phone:</label></td>
        <td><input type="text" name="ir_cell_phone" value="<?php echo $ir_cell_phone ?>" size="15" maxlength="15" /></td>
      </tr>
      <tr>
        <td class="labelRight"><label> Work Phone:</label></td>
        <td><input type="text" name="ir_work_phone" value="<?php echo $ir_work_phone ?>" size="15" maxlength="15" /></td>
      </tr>
      <tr>
        <td class="labelRight"><label> E-Mail:</label></td>
        <td><input type="text" name="ir_email" value="<?php echo $ir_email ?>" size="20" maxlength="75" /></td>
      </tr>
      <tr>
        <td colspan="2" align="center"><input type="submit" name="submit" value="Submit" class="groovybutton" />
          <input type="button" value="Cancel" class="groovybutton" onClick="history.back()"></td>
      </tr>
     </tbody>
  </table>
  <?php if (isset($_GET['id'])) {?>
  <input type="hidden" name="id" value="<?php echo $id ?>" />
  <?php }
							//if admin display last editor and date
							if ($admin == 1) {
							/* format date for display */
    						$current_editdate = strtotime($editdate);
   							$displaydate = date('m/d/Y g:i:s A', $current_editdate);							
							echo "<table class = \"admintable\" width=\"500px\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">
								<tr>
									<td><strong>Last Editor:</strong></td><td>$lasteditor</td>
									<td><strong>Last Edit Date/Time:</strong></td><td>$displaydate</td>
					</table>	</tr>";  } ?>
</form>
<?php /* meetings query */
	  if ($id)
		{ 
					$meetings_sql = "SELECT meetings.id AS id, day, mtime, city, name, code, open_closed, men_women, hc 
									FROM meetings,groups,locations,meeting_types 
									WHERE group_id=groups.id and location_id=locations.id and mtg_type_id=meeting_types.id and group_id=$id 
									ORDER BY day ASC, mtime ASC, city ASC";
					$meetings_result = mysql_query($meetings_sql)
					or die(mysql_error());
	if ($return_to!='intergroup_reps.php')
		{
		$num_rows = mysql_num_rows($meetings_result);
														}?>
<table class="tabletext">
  <thead class="meetingHeader">
    <tr class="titleRow">
      <th colspan="9"><h3>Meetings&nbsp;&nbsp;&nbsp;(<?php echo $num_rows ?>)</h3></th>
    </tr>
    <tr class="alternateRow">
      <th>Day/Time</th>
      <th>City</th>
      <th>Location</th>
      <th>Type</th>
      <th>O/C</th>
      <th>M/W</th>
      <th>HC</th>
      <th></th>
    </tr>
  </thead>
  <?php 
			$i = 0;
			while ($meetings_row = mysql_fetch_assoc($meetings_result))
			{
				// Set $longday
				switch ($meetings_row[day])
				{
				case 1:
					$longday="Sunday";
					break;
				case 2:
					$longday="Monday";
					break;
				case 3:
					$longday="Tuesday";
					break;
				case 4:
					$longday="Wednesday";
					break;
				case 5:
					$longday="Thursday";
					break;
				case 6:
					$longday="Friday";
					break;
				case 7:
					$longday="Saturday";
					break;
				}
		
			// Set Time variables for reading
			$meetings_time=$meetings_row['mtime'];
			$rawtime = strtotime($meetings_time);	
			$meetings_time = date('g:iA',$rawtime);
			$city=$meetings_row['city'];
			$group_id=$meetings_row['group_id'];			
			$location_name=$meetings_row['name'];
			$meeting_types_code=$meetings_row['code'];
				
				
			if ($i%2 == 0) 
			{
			$rowcolor="$reprowcolor";
			} else 
			{
			$rowcolor="white";
			}
			?>
  <tr bgcolor="<?php echo $rowcolor?>">
    <td><div class="meetingTime"><?php echo "$longday-$meetings_time";?></div></td>
    <td><div class="meetingCity"><?php echo "$city";?></div></td>
    <td><div class="meetingLocation"><?php echo "$location_name";?></div></td>
    <td><div class="meetingType"><?php echo "$meeting_types_code";?></div></td>
    <td><div class="meetingOpenClosed"><?php echo "$meetings_row[open_closed]";?></div></td>
    <td><div class="meetingMenWomen"><?php echo "$meetings_row[men_women]";?></div></td>
    <td><div class="meetingHC"><?php echo "$meetings_row[hc]";?></div></td>
    <td><div class="meetingDelete"> <a href="meetings.php?page=meeting_edit&id=<?php echo $meetings_row['id']?>&return_to=meetings.php?page=group_edit&group_id=<?php echo $id?>"><img src="../images/edit_button.png"></a> <a title="Delete" href="deleteform.php?id=<?php echo $meetings_row['id']?>&table=meetings&return_to=meetings.php?page=group_edit&group_id=<?php echo $id?>"><img src="../images/delete_button.png"></a> </div></td>
  </tr>
  <?php 
										
	$i++;	
	// While (meetings) loop end					
			}
echo "<tr><td><p><br /></p></td></tr></table>";
// IF meetings end
	}}
?>
</table>
<?php 
mysql_close($conn); ?>
